package com.yang.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.yang.dao.TestDao;
import com.yang.pojo.TestDto;

@Repository
public class TestDaoImpl implements TestDao{
	@Resource
	private JdbcTemplate jdbcTemplate;
	public List<TestDto> test(String id) {
		int ID = Integer.parseInt(id);
		String sql = "SELECT name,cla FROM jake WHERE id =?";
		ResultSetExtractor<List<TestDto>> list = new ResultSetExtractor<List<TestDto>>() {
			@Override
			public List<TestDto> extractData(ResultSet rs) throws SQLException,DataAccessException{
				List<TestDto> lists = new ArrayList<TestDto>();
				while(rs.next()) {
					String courseName = rs.getString("name");
					String teacherName = rs.getString("cla");
					TestDto teaAndCourseName = new TestDto(teacherName,courseName);
					lists.add(teaAndCourseName);
				}
				return lists;
			}
		};
		return jdbcTemplate.query(sql,new Object[] {ID},list);
	}
}
